using System;
using I2.Loc;
using UnityEngine;

public class PixelmonBehaviour : MonoBehaviour
{
	public string namePixelmon;

	private NJGMapItem _iconMiniMap;

	private PointerPanel _pointer;

	[SerializeField]
	private Transform _pointerOnPixelmon;

	[SerializeField]
	private GameObject _particle;

	private float lastVolume = -1f;

	private bool isGround;

	private float timerRay;

	public static event Action<string> OnPixelmonCollision;

	private void Start()
	{
		if (PixelmonController.instance != null)
		{
			PixelmonController.instance.actualPixelmons.Add(this);
			PixelmonController.instance.UpdateInterface();
			if (!PixelmonController.instance.singleMessagePixelmon.Contains(namePixelmon))
			{
				PixelmonController.instance.singleMessagePixelmon.Add(namePixelmon);
				GameplayNotificationController.instance.AddNotification(ScriptLocalization.Get("Game_Common/" + namePixelmon + "_name") + " " + ScriptLocalization.Get("Game_Common/Pixelmon_SpawnPixelmon"), 0);
			}
		}
		_iconMiniMap = base.gameObject.AddComponent<NJGMapItem>();
		_iconMiniMap.SetType("Pixelmon");
		if (PixelmonController.instance != null && !PixelmonController.instance.hasPixelmon)
		{
			_pointer = PointerController.instance.AddPointer(_pointerOnPixelmon, 0f, "PixelmoonIcpn_" + namePixelmon);
		}
		base.transform.SetParent(PixelmonController.instance.transform);
	}

	private void OnTriggerEnter(Collider other)
	{
		PlayerBehavior componentInParent = other.GetComponentInParent<PlayerBehavior>();
		if (componentInParent != null && componentInParent.isMine && !componentInParent.isDead && componentInParent.localTeam != 0 && PixelmonBehaviour.OnPixelmonCollision != null)
		{
			PixelmonBehaviour.OnPixelmonCollision(namePixelmon);
		}
	}

	private void OnEnable()
	{
		PixelmonController.MeCatchPixelmon += HidePointer;
		PixelmonController.MeDropPixelmon += ShowPointer;
	}

	private void OnDisable()
	{
		PixelmonController.MeCatchPixelmon -= HidePointer;
		PixelmonController.MeDropPixelmon -= ShowPointer;
	}

	private void OnDestroy()
	{
		if (PixelmonController.instance != null)
		{
			PixelmonController.instance.actualPixelmons.Remove(this);
		}
		PixelmonController.MeCatchPixelmon -= HidePointer;
		PixelmonController.MeDropPixelmon -= ShowPointer;
		if (PixelmonController.instance != null)
		{
			PixelmonController.instance.ShowCapturedPixelmonParticle(base.transform.position);
		}
	}

	private void ShowPointer()
	{
		if (_pointer == null)
		{
			_pointer = PointerController.instance.AddPointer(_pointerOnPixelmon, 0f, "PixelmoonIcpn_" + namePixelmon);
		}
	}

	private void HidePointer()
	{
		PointerController.instance.RemovePointer(_pointer);
	}

	private void Update()
	{
		if (lastVolume != VolumeController.VolumeSound)
		{
			AudioSource[] components = GetComponents<AudioSource>();
			foreach (AudioSource audioSource in components)
			{
				audioSource.volume = VolumeController.VolumeSound;
				lastVolume = VolumeController.VolumeSound;
			}
		}
	}
}
